# -*- codeing = utf-8 -*-
from selenium import webdriver
import time
import csv
import string
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#创建一个options对象
#创建一个csv文件

#最开始需要获取的区域
address = input('请输入你要了解的房源地区：\n')
f = open(f'./{address}.csv','w',encoding='utf-8',newline='')
mywrite = csv.writer(f)
mywrite.writerow(['行政区','标题','小区','位置','户型','面积','装修','单价','总价'])

options = webdriver.ChromeOptions()
# 避免终端下执行代码报错
options.add_experimental_option("excludeSwitches", ['enable-automation',
'enable-logging'])
options = webdriver.ChromeOptions()
# 避免终端下执行代码报错
options.add_experimental_option("excludeSwitches", ['enable-automation',
'enable-logging'])
#创建浏览器对象，去请求链家地址
browser = webdriver.Chrome('./chromedriver.exe',options=options)
browser.maximize_window()
URL = f'https://cd.lianjia.com/ershoufang/pgrs{address}/'
browser.get(url=URL)
browser.find_element(By.CSS_SELECTOR, '#searchInput').send_keys(address)
# 点击搜索
browser.find_element(By.CSS_SELECTOR, '#searchForm > div > button').click()

#青羊区
for i in range(1,101):

    URL = f'https://cd.lianjia.com/ershoufang/pg{i}rs{address}/'
    browser.get(url=URL)

    #搜索框定位输入的区域

    #调用js方法，获取整个页面
    y = 0
    y_max = 7000
    while y < y_max:
        y += 500
        #调用js实现页面滚动刷新
        browser.execute_script(f'window.scrollTo(0,{y})')
        time.sleep(0.1)
    #获取整个列表
    house = browser.find_elements(By.CSS_SELECTOR,'#content > div.leftContent > ul > li')


    for i in house:
        if house !=None:

            address_xz = address
            title = i.find_element(By.CSS_SELECTOR,'div.info.clear > div.title > a').text.replace('|','  ')
            Dis = i.find_element(By.CSS_SELECTOR,'div.info.clear > div.flood > div > a').text.replace('|','  ')
            address_1 = i.find_element(By.CSS_SELECTOR,'div.info.clear > div.flood > div > a:nth-child(2)').text.replace('|','  ')
            huxing = (i.find_element(By.CSS_SELECTOR,'div.info.clear > div.address > div').text).replace('|','  ')[0:5]
            area = (i.find_element(By.CSS_SELECTOR, 'div.info.clear > div.address > div').text)[7:13]

            zhuangxiu = (i.find_element(By.CSS_SELECTOR, 'div.info.clear > div.address > div').text).replace('|','     ')[31:40].replace(' ','')
            #print(zhuangxiu,len(zhuangxiu))
            danjia = (i.find_element(By.CSS_SELECTOR, 'div.info.clear > div.priceInfo > div.unitPrice').text).replace('|','  ')
            zongjia = (i.find_element(By.CSS_SELECTOR, 'div.info.clear > div.priceInfo > div.totalPrice.totalPrice2').text)
            zongjia_1 = ''.join(zongjia.split())
            mywrite.writerow([address_xz, title,Dis,address_1,huxing,area,zhuangxiu,danjia,zongjia_1])

            #print(huxing,'****',area,'*******',zhuangxiu)

    #browser.switch_to.window(browser.window_handles[1])

f.close()


